using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MathExten;

public class PlayerControls : MonoBehaviour
{
    private Vector2 inputVector;
    private float inputVectorMagnitude, prevInputVecMag = 0;
    private Rigidbody2D playerRg;

    [SerializeField] private AnimationCurve StartCurve,StopCurve;

    [SerializeField] private float playerSpeedMultiplier;
    [SerializeField] private float playerFriction;
    private float EvaluatedMovementMultipier;
    // Start is called before the first frame update
    void Start()
    {
        playerRg = this.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        Movement();
        FrictionLoss();
        //Debug.Log(inputVectorMagnitude);
    }

    private void LateUpdate()
    {
        prevInputVecMag = inputVectorMagnitude;
    }

    void Movement()
    {
        Vector2 getInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        inputVectorMagnitude = Mathf.Abs(getInput.magnitude.Map(0f,Mathf.Sqrt(2),0f,1f));
        inputVector = getInput.normalized;
        //Mapping the 0-1 , float type input into different curves
        if (prevInputVecMag < inputVectorMagnitude)
        {
            //Gaining Speed
            EvaluatedMovementMultipier = StartCurve.Evaluate(inputVectorMagnitude);
            //Debug.Log("Method Gain");
        }
        else if (prevInputVecMag > inputVectorMagnitude)
        {
            //Losing Speed
            EvaluatedMovementMultipier = StopCurve.Evaluate(inputVectorMagnitude);
            //Debug.Log("Method Loss");
        }
        //Passing the speed info to body
        //playerRg.AddForce(inputVector*EvaluatedMovementMultipier*playerSpeedMultiplier*Time.deltaTime,ForceMode2D.Impulse);
        playerRg.velocity = (inputVector * EvaluatedMovementMultipier * playerSpeedMultiplier);
        Debug.Log(inputVector*EvaluatedMovementMultipier*playerSpeedMultiplier);
    }

    void FrictionLoss()
    {
        if (inputVectorMagnitude <= 0.05f)
            playerRg.velocity *= 0.1f;
    }
    
    
}
